//import Subscriber from '@ohos.commonevent';
import Subscriber from '@ohos.commonEvent';
import router from '@system.router';
import CheckEmptyUtils from '../../utils/CheckEmptyUtils.js';
import usb from '@ohos.usb';

let mCommonEventSubscriber = null;
let mCommonEventSubscribeInfo = {
    events: [Subscriber.Support.COMMON_EVENT_USB_DEVICE_DETACHED, Subscriber.Support.COMMON_EVENT_USB_DEVICE_ATTACHED,
    Subscriber.Support.COMMON_EVENT_USB_ACCESSORY_ATTACHED,
    Subscriber.Support.COMMON_EVENT_USB_ACCESSORY_DETACHED,
    Subscriber.Support.COMMON_EVENT_USB_STATE,]
};
let mIsSubscribed = false;
let timeout = function(time){
    return new Promise(function(resolve,reject){
        setTimeout(()=>{
            resolve()
        }, time)
    })
}



export default {
    data: {
        //插拔事件通知结果
        result1: '',
        //目前的function字符串形式与掩码形式
        currentFuncs: null,
        //device function字符串形式
        funcString:"",
        //device function掩码形式
        funcCode:0,
        //device function设置成功与否
        resultSet: true,
        //插拔事件通知上次结果
        Old: '',
        //插拔事件通知本次结果
        New: '',
        //按钮点击次数
        x: 0,
        //插事件次数
        y: 0,
        //拔事件次数
        z: 0,
        //插拔事件总次数
        s: 0,
        xtime: '',
        ytime: '',
        ztime: '',
        stime: '',
        //设置100次插拔，设置结果
        result2: ''
    },
    onInit() {
        this.subUsbListener();
        this.getFunctions();
    },

    subUsbListener() {
        console.info("usb subUsbListener....");
        if (mIsSubscribed) {
            console.info("usb event already subscribed");
            this.result1 = "已经监听了...不要重复点击"
            return;
        }
        Subscriber.createSubscriber(mCommonEventSubscribeInfo, this.CreateSubscriberCallBack.bind(this));

    },
    CreateSubscriberCallBack(err, data) {
        this.result1 = "开始监听..."
        console.info("usb CreateSubscriberCallBack....");
        mCommonEventSubscriber = data;
        Subscriber.subscribe(mCommonEventSubscriber, this.SubscriberCallBack.bind(this));
        mIsSubscribed = true;
    },
    SubscriberCallBack(err, data) {
        this.result1 = "监听....回调"
        console.info("usb subscriber call back")
        this.result1 = this.$t(JSON.stringify(data));
        console.info("usb=======================>SubscriberCallBack  data = " + JSON.stringify(data));
        console.info("usb=======================>SubscriberCallBack  event = " + data.event);
        this.New = this.result1;
        if (this.New.includes("ATTACHED")) {
           this.Old = this.New;
           this.y += 1;
           this.ytime = this.y + "次"
        }
        else
        {
            this.Old = this.New;
            this.z += 1;
            this.ztime = this.z + "次"
        };
        this.s = this.y + this.z;
        this.stime = this.s + "次"
    },
    unSubscriberListener() {
        if (!mIsSubscribed || CheckEmptyUtils.isEmpty(mCommonEventSubscriber)) {
            console.info("usb event not subscribed");
            return;
        }
        this.result1 = "取消监听...."
        console.info("usb event unsubscribed");
        Subscriber.unsubscribe(mCommonEventSubscriber, () => {
            console.info("usb event unsubscribed");
        });
        mIsSubscribed = false;
    },

    setResult(){
        if (this.funcString == "none"){
              this.funType = 4;
              this.setFunction(this.funType);
        }
        else{
              this.funType = 0;
              this.setFunction(this.funType);
          };
        this.x += 1;
        this.xtime = this.x + "次"
    },
    getFunctions() {
        console.info("usb getCurrentFunctions begin----");
        var funcCode = usb.getCurrentFunctions();
        this.funcString = usb.usbFunctionsToString(funcCode);
        this.currentFuncs = "function字符串形式:" + this.funcString + " \nfunction掩码形式: " + funcCode;
        console.info("usb getCurrentFunctions : " + this.currentFuncs);
    },
    functionStrToNum(){
        this.funcCode = usb.usbFunctionsFromString(this.funcString);
    },
    setFunction(funType) {
        console.info("usb setCurrentFunctions begin----");
        usb.setCurrentFunctions(funType).then(data => {
            console.info("usb setCurrentFunctions : " + data);
            this.resultSet = data;
            this.getFunctions();
        }).catch(error => {
            console.info("usb setCurrentFunctions error : " + error);
            this.resultSet = error;
            this.getFunctions();
        });
    },

    async set1(){
        this.result2 = "正在设置...";
        if(this.funcString == "none"){
            for ( let i = 1; i <= 5; i ++ ) {
                await timeout(1000).then(()=>{
                    this.funType = 4;
                    this.setFunction(this.funType);
                    return timeout(10000)
                }).then(()=>{
                    this.funType = 0;
                    this.setFunction(this.funType);
                    return timeout(6000)
                }).then(()=>{
                    this.result2 = "已设置" + i + "次";
                    return timeout(3000)
                })
            }
        }
        else{
            for ( let i = 1; i <= 5; i ++ ) {
                await timeout(1000).then(()=>{
                    this.funType = 0;
                    this.setFunction(this.funType);
                    return timeout(10000)
                }).then(()=>{
                    this.funType = 4;
                    this.setFunction(this.funType);
                    return timeout(6000)
                }).then(()=>{
                    this.result2 = "已设置" + i + "次";
                    return timeout(3000)
                })

            }
        };
        this.result2 = "设置结束";
    },

    onDestroy() {
        console.info("usb onDestroy");
        this.unSubscriberListener();
    },
    onBackPress() {
        router.back();
    },
}



